var t = require("../../../@babel/runtime/helpers/interopRequireDefault").default,
  e = require("../../../utils/http.js"),
  a = t(require("../../../utils/util")),
  n = new e.HTTP;
  let {intention,increased }=require("../../../api/attract/select")
Page({
  data: {
    khlya:'',
    showKhlysm:false,
    value:'',
    radio:'1',
    selectArray: [{
      id: 1,
      text: "产业"
    }, {
      id: 2,
      text: "税收"
    }, {
      id: 3,
      text: "人才申报"
    }, {
      id: 4,
      text: "载体出租"
    }],
    sortType: 2,
    nowText: "税收",
    selectArrays: [{
      id: 1,
      text: "未定"
    }, {
      id: 2,
      text: "实体-新注册"
    }, {
      id: 3,
      text: "实体-迁址"
    }, {
      id: 4,
      text: "挂靠-新注册"
    }, {
      id: 5,
      text: "挂靠-迁址"
    }],
    sortTypes: 1,
    nowTexta: "未定",
    // yixiang: [
    //   {
    //     text:'联健元和',
    //     id:1
    //   }
    // ],
    // yixiangtype:1,

    nowTextb: "",
    showTab:false,
    selectText:'',
    colorText:'#666',
    kehuxingzhi: [{
      id: 1,
      text: "企业客户"
    }, {
      id: 2,
      text: "个人客户"
    }, {
      id: 3,
      text: "政府事业单位"
    }],
    kehutype: 1,
    nowTextc: "企业客户",
    yixaing: [],
    yixaingtype: null,
    nowTextd: "",
    laiyuan: [],
    laiyuantype: null,
    nowTexte: "",
    purpose: [],
    nowTextf: "请选择",
    goal: [{
      id: 1,
      text: "产业"
    }, {
      id: 2,
      text: "税收"
    }, {
      id: 3,
      text: "人才申报"
    }, {
      id: 4,
      text: "载体出租"
    }, {
      id: 5,
      text: "渠道"
    }],
    goals: 2,
    nowTexts: "税收",
    // fenji: [{
    //   id: 1,
    //   text: "一般客户"
    // }, {
    //   id: 2,
    //   text: "重点客户"
    // }],
    // fenjitype: 1,
    // nowTextj: "一般客户",
    infor: [],
    currentDate: (new Date).getTime(),
    minDate: new Date(2023, 0, 1).getTime(),
    formatter: function (t, e) {
      return "year" === t ? "".concat(e, "年") : "month" === t ? "".concat(e, "月") : e
    },
    show: !1,
    shows: !1
  },
  selectKhly(e){
    this.setData({
      khlya:e.detail.value
    })
   
  },
  clickKhly(e){
    this.setData({
      khlya:e.currentTarget.dataset.khlya
    })
  },
  onClickKhlysm(){
    console.log(this.data.nowTexte)
    if(this.data.nowTexte==''){
      wx.showToast({
        title: '请选择渠道来源',
        icon:'none',
        duration:1e3
      })
    }else{
      intention(3,this.data.laiyuantype).then(res=>{
        res.data.forEach(item=>{
          item.text=item.khlysm
          item.id=item.khlysm
        })
        this.setData({
          khlysm: res.data,
          khlysmtype:1,
        })
  
      })
      this.setData({
      showKhlysm:!this.data.showKhlysm
    })
    }
    
  },
  onInput: function (t) {
    this.setData({
      currentDate: t.detail
    })
  },
  onConfirm: function (t) {
    var e = a.default.formatStamp(t.detail);
    this.setData({
      show: !1,
      yjdc: e
    })
  },
  onCancel: function () {
    this.setData({
      show: !1,
      ydsj: "请选择预定达成的日期"
    })
  },
  opentime: function () {
    this.setData({
      show: !0
    })
  },
  selectfenji: function (t) {
    this.setData({
      fenjitype: t.detail.selectedID,
      nowTextj: t.detail.nowText
    })
  },
  selectEvent: function (t) {
    this.setData({
      sortType: t.detail.selectedID,
      nowText: t.detail.nowText
    })
  },
  selectEvent2: function (t) {
    this.setData({
      sortTypes: t.detail.selectedID,
      nowTexta: t.detail.nowText
    })
  },
  selectEvent3: function (t) {
    this.setData({
      yixiangtype: t.detail.selectedID,
      nowTextb: t.detail.nowText
    })
  },
  selectEvent4: function (t) {
    this.setData({
      kehutype: t.detail.selectedID,
      nowTextc: t.detail.nowText
    })
  },
  // 意向等级
  selectEvent5: function (t) {
    this.setData({
      yixaingtype: t.detail.selectedID,
      nowTextd: t.detail.nowText
    })
  },
  selectEvent6: function (t) {
    this.setData({
      laiyuantype: t.detail.selectedID,
      nowTexte: t.detail.nowText
    })
  },
  selectEvent7: function (t) {
    this.setData({
      sections: t.detail.selectedID,
      nowTextf: t.detail.nowText
    })
  },
  selectEvent8: function (t) {
    this.setData({
      goals: t.detail.selectedID,
      nowTexts: t.detail.nowText
    })
  },
 
  onChangeRadio(e){
    this.setData({
      radio:e.detail,
      iphonei:''
    })
  },
  preventTouchMove: function (t) {},
  
  formSubmits: function (t) {
    // 手机号码微信号校验
    let {iphonenum} = t.detail.value
    if (this.data.radio === '1') {
      if (iphonenum.length !== 0) {
        const reg = /^1\d{10}$/
        if (!reg.test(iphonenum)) {
          return wx.showToast({
            title: '手机号码格式有误',
            icon:'none',
            duration:1e3
          })
        }
      }else{
        return wx.showToast({
          title: '手机号码不能为空',
          icon:'none',
          duration:1e3
        })
      }
    }else{
      if (iphonenum.length !== 0) {
        if (iphonenum.length < 4) {
          return wx.showToast({
            title: '微信号必须在4位字符及以上',
            icon:'none',
            duration:1e3
          })
        }
      }else{
        return wx.showToast({
          title: '微信号不能为空',
          icon:'none',
          duration:1e3
        })
      }
    }
    
    var e = this,
    a = t.detail.value;

    null == e.data.tname ? wx.showToast({
      title: "尚未登录请联系数据中心",
      icon: "success",
      duration: 1e3,
      success: function () {
        setTimeout((function () {
          wx.navigateBack({
            delta: 1
          })
        }), 1e3)
      }
    }) : wx.showModal({
      title: "提示",
      content: "请核对数据是否准确",
      success: function (t) {
        if (t.confirm)
          if (
            // a.mianji ? e.setData({
            //   xuqiumj: a.mianji
            // }) : e.setData({
            //   xuqiumj: 0
            // }), a.yuji ? e.setData({
            //   yujishuis: a.yuji
            // }) : e.setData({
            //   yujishuis: 0
            // }), 
            a.qymc && a.lxr && a.iphonenum && e.data.nowText && "请选择" !=  e.data.nowTextd && e.data.nowTexte&&e.data.khlya
            ) {
            var i = new Date(+new Date + 288e5).toJSON().substr(0, 19).replace("T", " "),
              o = parseInt(1e5 * Math.random()),
              s = "kh" + i.slice(2, 4) + i.slice(5, 7) + i.slice(11, 13) + o;
            n.request({
              url: "https://www.qlsyyun.com:3030/zscb/map", // 3030
              data: {
                qymc: a.qymc
              }
            }).then((function (t) {
              t.data.length > 0 ? (wx.showToast({
                title: "该企业已被录入",
                icon: "none",
                duration: 1e3
              }), wx.pageScrollTo({
                scrollTop: 0
              })) : increased({
                qymc: a.qymc,
                lxr: a.lxr,
                lxphone: a.iphonenum,
                zt: e.data.tname,
                khbh: s,
                bm: e.data.bumen,
                bbsj: e.data.bbsj,
                khlysm:e.data.khlya,
                khly:e.data.laiyuantype,
                khyxdj:e.data.yixaingtype,
              }).then((function (t) {
                var o = i.slice(2, 4) + i.slice(5, 7) + 1;
                n.request({
                    url: "https://www.qlsyyun.com:3000/increased/zsjd", // 3000
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    data: {
                      khbh: s,
                      qymc: a.qymc,
                      bm: e.data.bm,
                      zsy: e.data.tname,
                      tjsj: e.data.bbsj,
                      tname: e.data.tname,
                      khzbbh: o
                    }
                  }).then((function (t) {})),
                  n.request({
                    url: "https://www.qlsyyun.com:3000/increased/lxrb", // 3000
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    data: {
                      flag:e.data.radio,
                      khbh: s,
                      qymc: a.qymc,
                      lxr: a.lxr,
                      lxphone: a.iphonenum,
                      tjsj: e.data.bbsj,
                      tname: e.data.tname,
                    }
                  }).then((function (t) {})),
                  n.request({
                    url: "https://www.qlsyyun.com:3030/clock/add", // 3030
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    data: {
                      qymc: a.qymc,
                      bm: e.data.bm,
                      zsy: e.data.tname
                    }
                  }).then((function (t) {})),
                  n.request({
                    url: "https://www.qlsyyun.com:3000/increased/stjd", // 3000
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    data: {
                      khbh: s,
                      qymc: a.qymc,
                      bm: e.data.bm,
                      zsy: e.data.tname
                    }
                  }).then((function (t) {})),
                  n.request({
                    url: "https://www.qlsyyun.com:3000/increased/stmx", // 3000
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    method: "POST",
                    data: {
                      qymc: a.qymc,
                      zsy: e.data.tname,
                      bm: e.data.bm,
                      khbh: s,
                      tjsj: e.data.bbsj,
                      yjss: e.data.yujishuis
                    }
                  }).then((function (t) {})),
                  wx.removeStorageSync("namei"),
                  wx.removeStorageSync("abbri"),
                  wx.removeStorageSync("linki"),
                  wx.removeStorageSync("iphonei"),
                  wx.removeStorageSync("areai"),
                  wx.removeStorageSync("salei"),
                  wx.removeStorageSync("lasti"),
                  wx.removeStorageSync("expecti"),
                  wx.removeStorageSync("websitei"),
                  wx.removeStorageSync("briefi"),
                  wx.removeStorageSync("notesi"),
                  wx.removeStorageSync("nowaddress"),
                  wx.removeStorageSync("address"),
                  
                  wx.showModal({
                    title: "提示",
                    content: "下一步",
                    confirmText:'完善信息',
                    cancelText:'客户列表',
                    success: function (t) {
                      t.confirm ? n.request({
                        url: "https://www.qlsyyun.com:3030/lease/page", // 3030
                        data: {
                          arr: 6,
                          khbh: s
                        }
                      }).then((function (t) {
                        n.request({
                          url: "https://www.qlsyyun.com:3000/information", // 3000
                          data: {
                            bID: t.data[0].bID
                          }
                        }).then((function (t) {
                          e.setData({
                              infor: t.data[0]
                            }),
                            wx.setStorageSync("infor", e.data.infor),
                            wx.redirectTo({
                              url: "/pages/intention/specifics/specifics?bID=" + t.data[0].bID+"&id=1"
                            })
                        }))
                      })) : setTimeout((function () {
                        wx.redirectTo({
                          url: "/pages/intention/intention/intention?address=" + '我的客户' + "&id=" + 3,
                        })
                      }), 1e3)
                    }
                  })
              }))
            }))
          } else
            wx.showToast({
              title: "请检查是否有必填项未填",
              icon: "none",
              duration: 1e3
            })
      }
    })
  },
  quxiao: function () {
    wx.navigateBack({
      delta: 1
    })
  },


  onTab(){
    this.setData({
      showTab: ! this.data.showTab
    })
  },

  onSelect(event){
    var index = event.currentTarget.dataset.index;
    var tabs = this.data.yixiang;
    for (var i = 0; i < tabs.length; i++) {
      if (i === index) {
        tabs[i].color = '#3072f6';
        this.setData({
          selectText:tabs[i].text
        })
        console.log(this.data.selectText);
      } else {
        tabs[i].color = '#666';
      }
    }
    this.setData({ yixiang: tabs }); // 更新数据
  },

  swapFirstElementWithOther(arr, index) {
    if (index >= 0 && index < arr.length) {
      const temp = arr[0];
      arr[0] = arr[index];
      arr[index] = temp;
    }
    return arr;
  },

  // 清缓存用
  onUnload(){
    wx.removeStorageSync("iphonei")
  },
  onLoad: function (t) {
    var e = this;
    intention(1).then(res=>{
      res.data.forEach(item=>{
        item.text=item.nr
        item.id=item.na
      })
      e.setData({
        yixaing: res.data,
        yixaingtype:1
      })
    })
    intention(2).then(res=>{
      res.data.forEach(item=>{
        item.text=item.nr
        item.id=item.nr
      })
      e.setData({
        laiyuan: res.data,
        laiyuantype:1
      })
    })
    intention(3).then(res=>{
      res.data.forEach(item=>{
        item.text=item.khlya
        item.id=item.khlya
      })
      e.setData({
        khlysm: res.data,
        khlysmtype:1
      })
    })
    n.request({
        url: "https://www.qlsyyun.com:3000/enterproject" // 3000
      }).then((function (t) {
        e.setData({
          yixiang: t.data,
          yixiangtype:1
        })
        let arr = e.data.yixiang
        arr.forEach((item,index)=>{
          Object.assign(item,{
            color: item.text === wx.getStorageSync('user')[1] ? '#3072f6' : '#666',
            index:index
          })
        })

        if (!arr.some(item=>item.text === wx.getStorageSync('user')[1])) {
          e.setData({
            selectText:arr[0].text,
          })
          arr[0].color = '#3072f6'
          
        }
        e.setData({
          yixiang:arr
        })
        
        let newArr = []
        e.data.yixiang.forEach(item=>{
          if (item.text === wx.getStorageSync('user')[1]) {
            newArr = e.swapFirstElementWithOther(arr,item.index)
            e.setData({
              selectText:item.text,
              yixiang:newArr
            })
          }else{
            e.setData({
              yixiang:arr
            })
          }
        })

      })),
      n.request({
        url: "https://www.qlsyyun.com:3030/sector" // 3030
      }).then((function (t) {
        e.setData({
          purpose: t.data
        })
        console.log(e.data.purpose)

      }));
    
    var i = wx.getStorageSync("namei"),
      o = wx.getStorageSync("abbri"),
      s = wx.getStorageSync("linki"),
      c = wx.getStorageSync("iphonei"),
      d = wx.getStorageSync("areai"),
      r = wx.getStorageSync("salei"),
      u = wx.getStorageSync("lasti"),
      l = wx.getStorageSync("expecti"),
      w = wx.getStorageSync("websitei"),
      x = wx.getStorageSync("briefi"),
      y = wx.getStorageSync("notesi"),
      h = wx.getStorageSync("infor"),
      m = wx.getStorageSync("user"),
      f = m[0];
    e.setData({
      infor: h,
      tname: f,
      bm: m[1],
      bumen: m[1],
      yjdc: a.default.formatDate(new Date),
      bbsj: a.default.formatDate(new Date),
      zsdq: a.default.formatExpect(new Date),
      namei: i,
      abbri: o,
      linki: s,
      iphonei: c,
      areai: d,
      salei: r,
      lasti: u,
      expecti: l,
      websitei: w,
      briefi: x,
      notesi: y
    })
    console.log(e.data);
  },
  onShow: function () {
    var t = wx.getStorageSync("address"),
      e = wx.getStorageSync("orient");
    this.setData({
      address: t,
      orient: e
    })
  },
  nameinput: function (t) {
    // wx.setStorageSync("namei", t.detail.value)
  },
  // abbrinput: function (t) {
  //   wx.setStorageSync("abbri", t.detail.value)
  // },
  linkinput: function (t) {
    wx.setStorageSync("linki", t.detail.value)
  },
  iphoneinput: function (t) {
    wx.setStorageSync("iphonei", t.detail.value)
  },
  areainput: function (t) {
    wx.setStorageSync("areai", t.detail.value)
  },
  saleinput: function (t) {
    wx.setStorageSync("salei", t.detail.value)
  },
  lastinput: function (t) {
    wx.setStorageSync("lasti", t.detail.value)
  },
  expectinput: function (t) {
    wx.setStorageSync("expecti", t.detail.value)
  },
  websiteinput: function (t) {
    wx.setStorageSync("websitei", t.detail.value)
  },
  briefinput: function (t) {
    wx.setStorageSync("briefi", t.detail.value)
  },
  notesinput: function (t) {
    wx.setStorageSync("notesi", t.detail.value)
  }
});